﻿using System;
using System.Windows;
using System.Windows.Controls;
using nRoute.Components;
using nRoute.Navigation;
using nRoute.Navigation.Containers;
using nRoute.Samples.FutureDesktop.Model;

namespace nRoute.Samples.FutureDesktop
{
    public partial class Workspaces : UserControl
    {
        private INavigationContainer _container;

        public Workspaces()
        {
            InitializeComponent();
            
            this.SetRelayConverter<object, string>("DateTimeConverterRelay", (o) => DateTime.Today.ToString("dd"));
            this.SetRelayConverter<Workspace, string>("ActiveWorkspaceUrlConverterRelay", (w) =>
            {
                if (_container != null) _container.Navigated -= new EventHandler<NavigatedEventArgs>(ActiveContainer_Navigated);

                _container = NavigationService.DefaultNavigationHandler as INavigationContainer;
                if (_container == null) return string.Empty;
                _container.Navigated += new EventHandler<NavigatedEventArgs>(ActiveContainer_Navigated);

                this.NavigateUrlText.Text = _container.Url;
                return _container.Url;
            });
        }

#region Hanlders

        private void ActiveContainer_Navigated(object sender, NavigatedEventArgs e)
        {
            this.NavigateUrlText.Text = e.Request.RequestUrl;
        }

        private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            RecalculateClipping();
        }

#endregion

#region Helpers

        private void RecalculateClipping()
        {
            // this is for the layout grid clipping
            this.LayoutGridRoundedClipRect.Rect = new Rect(0, 0, this.LayoutRoot.ActualWidth, this.LayoutRoot.ActualHeight);
            this.LayoutGridRoundedClipRect.RadiusX = 100;    // 1 less       
            this.LayoutGridRoundedClipRect.RadiusY = 100;    // 1 less
            this.LayoutGridLeftCurveClipRect.Rect = new Rect(0, 0, this.LayoutRoot.ActualWidth, 200);
            this.LayoutGridLeftCurveClipRect.RadiusX = 58;      // 2 less
            this.LayoutGridLeftCurveClipRect.RadiusY = 58;      // 2 less

            // this is for the content grid clipping
            this.ContentGridRoundedClipRect.Rect = new Rect(0, 0, this.ContentGrid.ActualWidth, this.ContentGrid.ActualHeight);
            this.ContentGridRoundedClipRect.RadiusX = 50;      // 2 less
            this.ContentGridRoundedClipRect.RadiusY = 50;      // 2 less
            this.ContentGridSquareClipRect.Rect = new Rect(0, 0, this.ContentGrid.ActualWidth, 100);

            this.ContentGridBottomSquareClipRect.RadiusX = 0;
            this.ContentGridBottomSquareClipRect.RadiusY = 0;
            this.ContentGridBottomSquareClipRect.Rect = new Rect(this.ContentGrid.ActualWidth - 75, this.ContentGrid.ActualHeight - 75, 75, 75);
        }

#endregion

    }
}
